package com.chenyue.cm.user.service.impl;


import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.StringUtils;
import com.chenyue.cm.common.vo.Page;
import com.chenyue.cm.user.domain.JdAppUser;
import com.chenyue.cm.user.domain.JdGoodsSku;
import com.chenyue.cm.user.domain.JdOrderComment;
import com.chenyue.cm.user.domain.vo.JdCollectSVo;
import com.chenyue.cm.user.domain.vo.JdGoodDetailVo;
import com.chenyue.cm.user.domain.vo.SkuVo;
import com.chenyue.cm.user.service.JdAppUserService;
import com.chenyue.cm.user.service.JdGoodsSkuService;
import com.chenyue.cm.user.service.JdOrderCommentService;
import com.chenyue.cm.utils.FreeMarkerUtils;
import freemarker.template.TemplateException;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Qualifier;
import com.chenyue.cm.user.dao.JdGoodsSpuMapper;
import com.chenyue.cm.user.domain.JdGoodsSpu;
import com.chenyue.cm.user.service.JdGoodsSpuService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;


/**
 * @author chenyue-cm
 */
@Service(value = "JdGoodsSpuService")
public class JdGoodsSpuServiceImpl extends BaseServiceImpl<JdGoodsSpu> implements JdGoodsSpuService {

    private final JdGoodsSpuMapper jdGoodsSpuMapper;
    @Resource
    private JdGoodsSkuService jdGoodsSkuService;
    @Resource
	private JdOrderCommentService jdOrderCommentService;
    @Resource
    private JdAppUserService jdAppUserService;

    @Resource
    private JdbcTemplate jdbcTemplate;

    public JdGoodsSpuServiceImpl(@Qualifier("jdGoodsSpuMapper") JdGoodsSpuMapper jdGoodsSpuMapper) {
        this.jdGoodsSpuMapper = jdGoodsSpuMapper;
    }

    @Override
    @Resource(name = "jdGoodsSpuMapper")
    public void setBaseMapper(LcBaseMapper<JdGoodsSpu, Serializable> baseMapper) {
        this.baseMapper = baseMapper;
    }


    @Override
    public List<JdCollectSVo> getHomePageGoodsList(String classifyId, String keyWord) {
        return jdGoodsSpuMapper.getHomePageGoodsList(classifyId, keyWord);
    }

    @Override
    public long getHomePageGoodsCount(String classifyId, String keyWord) {
        return jdGoodsSpuMapper.getHomePageGoodsCount(classifyId, keyWord);
    }

    @Override
    public JdGoodDetailVo getGoodsDetail(String goodsId) {
        //根据商品id获取商品信息
        JdGoodsSpu goodsSpu = jdGoodsSpuMapper.selectById(goodsId);
        //商品浏览量
        goodsSpu.setReadNum(goodsSpu.getReadNum() == null ? 0 : goodsSpu.getReadNum() + 1);

        Map<String, java.lang.Object> aMap = JSON.parseObject(JSON.toJSONString(goodsSpu));
        Map<String, Object> map = new HashMap<>();
        //商品id
        map.put("goodsId", goodsSpu.getId());
        //商品名称
        map.put("goodsName", goodsSpu.getGoodName());
        //商品主图
        map.put("imgUrl", goodsSpu.getImgUrl());
        //商品轮播图
        List<String> imgArry = Arrays.asList(goodsSpu.getImgArr().split(","));
        map.put("imgArr", imgArry);
        //商品浏览量
        map.put("readNum", goodsSpu.getReadNum());
        //获取商品销量
        map.put("sales", goodsSpu.getExchange());
        //获取商品库存
        map.put("inventory", jdGoodsSkuService.getInventory(goodsId));
        //积分
        Integer goodPrice = goodsSpu.getIntegral();
        map.put("integral", goodPrice == null ? "100000000000" : goodPrice);
        //商品详情
        map.put("goodsDetail", goodsSpu.getDetails());
        //获取商品sku列表
        List<JdGoodsSku> goodsSkuList = jdGoodsSkuService.getMGoods(goodsId);
        //定义 skuList 信息
        List<Map<String, java.lang.Object>> aList = new ArrayList<>();
        if (goodsSkuList != null && goodsSkuList.size() > 0) {
            for (JdGoodsSku sku : goodsSkuList) {
                Map<String, java.lang.Object> bMap = JSON.parseObject(JSON.toJSONString(sku));
                String attrInfo = sku.getAttrInfo();
                if (StringUtils.isNotEmpty(attrInfo)) {
                    List<Map> array = JSONObject.parseArray(attrInfo, Map.class);
                    for (Map<String, java.lang.Object> kmap : array) {
                        bMap.put(kmap.get("name").toString(), kmap.get("value").toString());
                    }
                }
                aList.add(bMap);
            }
        }

        String goodsAttString = goodsSpu.getUpdateUser();
        JSONArray array = JSON.parseArray(goodsAttString);
        List<SkuVo> list = new ArrayList<>();
        if(array!=null && array.size()>0){
            for(int i=0;i<array.size();i++){
                SkuVo skuVo = new SkuVo();
                JSONObject job = array.getJSONObject(i);
                String attrName = job.getString("attrName");
                String[] values = job.getString("values").replace("[","").replace("]","").replace("\"","").split(",");
                List<String> attrValue = Arrays.asList(values);
                skuVo.setName(attrName);
                skuVo.setAttr(attrValue);
                list.add(skuVo);
            }
        }

        map.put("attrList",list);

//        //获取评论内容和评论总数
//        EntityWrapper<MOrderComment> em = new EntityWrapper();
//        em.eq("goods_id", goodsId);
//        Integer commentCount = mOrderCommentMapper.selectCount(em);
//        //评论总数
//        map.put("commentCount", commentCount);
//        em.eq("show_flag", 1);
//        em.orderBy("create_time", false);
//        em.last("limit 0,1");
//        List<MOrderComment> commentList = mOrderCommentMapper.selectList(em);
//        commentList.stream().forEach(comment -> {
//            MMemberInfo mMemberInfo = mMemberInfoMapper.selectById(comment.getMemberId());
//            comment.setMemberPic(mMemberInfo.getPhoto());
//            List<String> commentPicArry = new ArrayList<>();
//            if(StringUtils.isNotBlank(comment.getCommentPics())) {
//                commentPicArry = Arrays.asList(comment.getCommentPics().split(","));
//            }else{
//                comment.setCommentPics("");
//            }
//            comment.setCommentPicArry(commentPicArry);
//        });
//        //详情展示评论
//        map.put("commentList", commentList);
//        map.put("state","false");
//
//        SysDicValueExample dicValueExample = new SysDicValueExample();
//        dicValueExample.createCriteria().andCodeEqualTo("tel");
//        String custServicePhone = sysDicValueService.selectByExample(dicValueExample).get(0).getName();
//        map.put("phone",custServicePhone);
        JdGoodDetailVo vo = JSON.parseObject(JSON.toJSONString(map), JdGoodDetailVo.class);
        return vo;

    }

}
